package com.happy.study.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.happy.study.entity.QuestionEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface QuestionMapper extends BaseMapper<QuestionEntity> {

    @Select (value = "<script>select qi.id,\n" +
            "       qi.title,\n" +
            "       qi.subTitle,\n" +
            "       qi.content,\n" +
            "       qi.categoryId,\n" +
            "       ci.categoryName AS categoryName,\n" +
            "       qi.userId,\n" +
            "       ui.nickName     AS nickName,\n" +
            "       fi.fileUrl AS headImgUrl,\n" +
            "       qi.createTime,\n" +
            "       qi.changeTime,\n" +
            "       qi.isUsed\n" +
            "from question_info qi\n" +
            "         left join category_info ci on ci.id = qi.categoryId\n" +
            "         left join user_info ui on qi.userId = ui.id\n" +
            "         left join file_info fi on ui.id = fi.userId <where><if test='categoryId != null and categoryId != 0'>and qi.categoryId=#{categoryId}</if></where> order by qi.createTime desc limit #{start},#{end}</script>")
    List<QuestionEntity> queryList ( @Param ("title") String title, @Param ( "categoryId" ) int categoryId, @Param ( "start" ) int start, @Param ( "end" ) int end );


    @Select (value = "SELECT qi.id,\n" +
            "       qi.title,\n" +
            "       qi.subTitle,\n" +
            "       qi.content,\n" +
            "       qi.categoryId,\n" +
            "       ci.categoryName AS categoryName,\n" +
            "       qi.createTime,\n" +
            "       qi.changeTime,\n" +
            "       qi.isUsed\n" +
            "FROM question_info qi left join category_info ci on qi.categoryId=ci.id\n" +
            "WHERE qi.id=#{id}")
    QuestionEntity querySingle ( int id );

}
